package service;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.telkomsigma.hibernate.persistence.player.entity.Player;
import com.telkomsigma.hibernate.services.player.PlayerService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class TestPlayerService extends AbstractTransactionalJUnit4SpringContextTests{
	
	@Autowired
	private PlayerService playerService;	

	@Test
	public void testCheckDaoConfig() {				
		assertNotNull(playerService);		
	}
	
	@Test
	public void testFindAllPlayer(){		
		List<Player> playerList = playerService.findAll(0,20);
		assertTrue(playerList.size() > 1);		
	}
	
	@Test
	public void testFindById(){
		Player player = playerService.findById("402880ea3192d999013192d99d150001");
		String lastName = player.getLastName();
		assertEquals("Mahendra", lastName);
	}
	
	@Test
	public void testFindByProperty(){
		String teamId = "ff8080813192ee1e013192ee203b0001";
		List<Player> playerList = playerService.findByProperty("teamId", teamId, 0,5);
		for (Player player : playerList){
			if (player.getReserve() == 0){ //playaer
				System.out.println("NAMA ASU PLAYER: " + player.getFirstName() + " " + player.getLastName());
			}else{
				System.out.println("NAMA ASU RESERVE: " + player.getFirstName() + " " + player.getLastName());
			}
		}
		assertNotNull(playerList);
		assertTrue(playerList.size() > 1);
	}
	
	@Test
	public void testInsertPlayer(){			
		Player player = new Player();		
		player.setFirstName("I Gede");
		player.setLastName("Mahendra");
		player.setTeamId("721873dgdsa23msd");
		player.setJoinDate(new Date());
		player.setReserve(0);
		player.setSalary(1000D);
		player.setCommision(0D);
		player.setPhotoId(null);
		Player result = playerService.insert(player);
		assertNotNull(result);
	}	
	
	@Test
	public void testUpdatePlayer(){
		Player player = playerService.findById("402880ea3192d999013192d99d150001");
		player.setLastName("Dein ne");
		player.setPlayerId("402880ea3192d999013192d99d150001");
		Player result = playerService.update(player);
		assertNotNull(result);
	}
	
	@Test
	public void testRemovePlayer(){
		Player player = new Player();
		player.setPlayerId("402880ea3192d999013192d99d150001");
		boolean check = playerService.remove(player);
		assertTrue(check == true);
	}	
}
